package com.bianguo.android.tencentpushdemo.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.entity.ItemData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/28.
 */

public class VLayoutAdapter extends VirtualLayoutAdapter<VLayoutAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<ItemData> list;
    private LayoutInflater inflater;
    private OnLongClickItemListener onLongClickItemListener;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnLongClickItemListener getOnLongClickItemListener() {
        return onLongClickItemListener;
    }

    public void setOnLongClickItemListener(OnLongClickItemListener onLongClickItemListener) {
        this.onLongClickItemListener = onLongClickItemListener;
    }

    public VLayoutAdapter(@NonNull VirtualLayoutManager layoutManager, Context mContext, ArrayList<ItemData> list) {
        super(layoutManager);
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    public VLayoutAdapter(@NonNull VirtualLayoutManager layoutManager) {
        super(layoutManager);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_reply_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ItemData itemData = list.get(position);
        String s = itemData.getContent();
        holder.contentTv.setText(s);
        holder.imageView.setImageResource(itemData.getImageId());
        holder.itemLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onLongClickItemListener != null) {
                    onLongClickItemListener.onLongClickItem(holder);
                }
                return true;
            }
        });
        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
//        List<LayoutHelper> helpers = getLayoutHelpers();
//        if (helpers == null) {
//            return 0;
//        }
//        int count = 0;
//        int size = helpers.size();
//        for (int i = 0; i < size; i++) {
//            int itemCount = helpers.get(i).getItemCount();
//            count += itemCount;
//        }
//        return count;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_reply_content)
        TextView contentTv;
        @BindView(R.id.item_reply_layout)
        LinearLayout itemLayout;
        @BindView(R.id.item_reply_image)
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnLongClickItemListener {
        void onLongClickItem(ViewHolder vh);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
